let workbenchBtnService = {
  computed: {
    // 登录用户id
    userId() {
      return this.$store.state.userInfo.staffId || ''
    },
    // 周转进度
    batchProcessStatus() {
      let list = this.$store.state.dict.batchProcessStatus.labels || []
      return list.filter((item) => item.value === 'ToBeStarted' || item.value === 'Producing' || item.value === 'Finished')
    },
    // 周转进度生产中、已完成状态的ID
    batchProcessStatusIds() {
      return this.batchProcessStatus.reduce((pre, cur) => [...pre, cur.id], []).join(',')
    },
    // 专检id
    inspectionId() {
      let list = this.$store.state.dict.inspectionType.labels || []
      let item = list.find(item => item.value === 'Special') || {id: ''}
      return item.id
    },
    // 验证是否已选
    _selected() {
      if (!this.selectData.length) return {msg: '请选择一条工序！'}
    },
    // 参数记录
    paramBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择工序进度非"待开工、未到达" , 工序名称相同 , 有未记录模板且模板名称相同的工序'
      if (this.selectData.some((item) => item.workstageName !== this.selectData[0].workstageName)) {
        status = true
      }
      if (this.selectData.some((item) => item.workstageProcessStatusValue === 'ToBeStarted' || item.workstageProcessStatusValue === 'NotArrive')) {
        status = true
      }
      if (this.selectData.some((item) => item.paramTemplateName !== this.selectData[0].paramTemplateName)) {
        status = true
      }
      if (this.selectData.some((item) => !item.paramTemplateId)) {
        status = true
      }
      if (this.selectData.length > 1 && this.selectData.some((item) => item.parameterRecordId)) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 检验完工
    completeBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const turnoverIds = this.selectData.map((item) => item.turnoverId)
      const clearArr = Array.from(new Set(turnoverIds))
      const msg = '请选择不同周转批次且周转状态为"正常" , 工序进度为"已开工"的工序'
      if (this.selectData.some((item) => item.turnoverStatusValue !== 'Normal')) {
        status = true
      }
      if (this.selectData.some((item) => item.workstageProcessStatusValue !== 'Started')) {
        status = true
      }
      if (this.selectData.some((item) => item.startWorkStaffId && item.startWorkStaffId !== this.userId)) {
        status = true
      }
      if (turnoverIds.length !== clearArr.length) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 开始检验
    startBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择周转状态为"正常" , 工序进度为"待开工"的工序'
      if (this.selectData.some((item) => item.turnoverStatusValue !== 'Normal')) {
        status = true
      }
      if (this.selectData.some((item) => item.workstageProcessStatusValue !== 'ToBeStarted')) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
    // 发起不合格申请
    unqualifiedBtnStatus() {
      if (this._selected) return this._selected
      let status = false
      const msg = '请选择周转状态为"正常"或"已暂停", 工序进度非"未到达"或"已完成"的工序'
      if (this.selectData.length > 1) {
        status = true
        return {disabled: status, msg: '请选择一条工序'}
      }
      if (this.selectData.find((item) => item.turnoverStatusValue !== 'Normal' && item.turnoverStatusValue !== 'Paused')) {
        status = true
      }
      if (this.selectData.find((item) => item.workstageProcessStatusValue === 'Finished' || item.workstageProcessStatusValue === 'NotArrive')) {
        status = true
      }
      if (this.selectData.some((item) => item.startWorkStaffId && item.startWorkStaffId !== this.userId)) {
        status = true
      }
      return status ? {disabled: status, msg: msg} : {msg: false}
    },
  },
  methods: {
    // 发起不合格申请
    unqualified() {
      this.mesConfirm('选中的记录将进行不合格申请单操作, 是否继续?', '不合格申请单提示').then(() => {
        this.$store.dispatch('saveData', {
          method: 'post',
          url: 'a/workstageproblemprocess/workstageProblemProcess/qmsprocess',
          data: {
            turnoverWorkstageId: this.selectData[0].id,
            viewSource: 2
          },
          noCommit: true
        }).then(() => {
          this.tipMessage('success', '提交成功')
          this.dialogVisible = false
          this.init()
        })
      })
    },
    // 参数记录
    paramRecord() {
      this.$refs.WorkStageParamRecord.openDialog(this.selectData)
    },
    /**
     * 检验完工
     */
    completeRegister() {
      this.$refs.CompleteRegister.openDialog(this.selectData, 'workShowInspection')
    },
    // 开工登记
    workStart() {
      if (this.selectData.some((item) => item.inputQuantity === 0)) {
        return this.tipMessage('error', '投料数量为0的工序不能开工，请通知班组长终止任务')
      }
      this.$refs.workStageStart.openDialog(this.selectData)
    },
  }
}
export default workbenchBtnService
